﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

using CSP.Core;

namespace CSP.Receivers
{
    public class FrameworkFeatureReceiver : SPFeatureReceiver
    {

        public void AddPermissionLevel(SPWeb web, string name, string description, SPBasePermissions permissions)
        {
            SPRoleDefinitionCollection collRoles = web.RoleDefinitions;
            SPRoleDefinition rd = new SPRoleDefinition();
            rd.Name = name;
            rd.Description = description;
            rd.BasePermissions = permissions;
            collRoles.Add(rd);
            web.Update();

        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            try
            {
                // feature is scoped at Site, so the parent is type SPSite rather than SPWeb..
                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    SPWeb currentWeb = null;
                    Guid gRootWebId = Guid.Empty;
                    if (site != null)
                    {
                        currentWeb = site.RootWeb;
                        gRootWebId = currentWeb.ID;
                    }
                    else
                    {
                        currentWeb = properties.Feature.Parent as SPWeb;
                        gRootWebId = currentWeb.Site.RootWeb.ID;
                    }

                    ResourceFile resFile = new ResourceFile(currentWeb.Locale.Name);

                    // First of all, Citizens and Civil Servants groups are created.
                    GroupsHelper.AddGroup(currentWeb, resFile.LoadResource("CSP_citizensgroup_title"), GroupsHelper.AssociatedGroupTypeEnum.Vistors, false);
                    GroupsHelper.AddGroup(currentWeb, resFile.LoadResource("CSP_civilservantsgroup_title"), GroupsHelper.AssociatedGroupTypeEnum.Members, false);

                    // Then, we create the custom permission level access for citizens.
                    AddPermissionLevel(currentWeb, resFile.LoadResource("CSP_citizenspermission_title"), resFile.LoadResource("CSP_citizenspermission_description"), SPBasePermissions.AddListItems);

                    // Owner of the site collection is added to the Civil Servant users group
                    currentWeb.Groups[resFile.LoadResource("CSP_civilservantsgroup_title")].AddUser(currentWeb.Site.Owner);
                    currentWeb.Groups[resFile.LoadResource("CSP_civilservantsgroup_title")].Update();
                    currentWeb.Groups[resFile.LoadResource("CSP_civilservantsgroup_title")].AddUser(currentWeb.Site.SystemAccount);
                    currentWeb.Groups[resFile.LoadResource("CSP_civilservantsgroup_title")].Update();
                    currentWeb.Update();

                    // Finally, anonymous users are allowed to navigate the portal
                    currentWeb.AnonymousState = SPWeb.WebAnonymousState.On;
                    currentWeb.Update();
                }
            }
            catch (Exception ex)
            {
                CSP.LoggingEngine.Utils.LogError(ex, this.GetType().Name);
            }
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {

                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    SPWeb currentWeb = null;
                    Guid gRootWebId = Guid.Empty;
                    if (site != null)
                    {
                        currentWeb = site.RootWeb;
                        gRootWebId = currentWeb.ID;
                    }
                    else
                    {
                        currentWeb = properties.Feature.Parent as SPWeb;
                        gRootWebId = currentWeb.Site.RootWeb.ID;
                    }

                    ResourceFile resFile = new ResourceFile();

                    currentWeb.Groups.Remove(resFile.LoadResource("CSP_citizensgroup_title"));
                    currentWeb.Groups.Remove(resFile.LoadResource("CSP_civilservantsgroup_title"));

                }
            }
            catch (Exception ex)
            {
                CSP.LoggingEngine.Utils.LogError(ex, this.GetType().Name);
            }

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

    }
}
